
///////////////////////////
// Kingdom_Province_Info //
// 0 = Male              //
// 1 = Female            //
// 2 = Monarch           //
// 3 = Protection        //
// 4 = Steward           //
///////////////////////////

var Kingdom_Total_Provinces  = 0 ;
var Kingdom_Total_Provinces2 = 0 ;

var Kingdom_Province_Info = new Array(25);
var Kingdom_Province_Inf2 = new Array(25);

var Kingdom_Province_Name = new Array(25);
var Kingdom_Province_Race = new Array(25);
var Kingdom_Province_Land = new Array(25);
var Kingdom_Province_NetW = new Array(25);
var Kingdom_Province_NWPA = new Array(25);
var Kingdom_Province_Rank = new Array(25);
var Kingdom_Province_Gain = new Array(25);

var Order_By_Name = new Array(25);
var Order_By_Race = new Array(25);
var Order_By_Land = new Array(25);
var Order_By_NetW = new Array(25);
var Order_By_NWPA = new Array(25);
var Order_By_Rank = new Array(25);
var Order_By_Gain = new Array(25);

var Kingdom_Currently_at_WAR = "" ;

function Clipboard_Kingdom() {

var Kingdom_Name             = "" ;
var Kingdom_Attitude_Us      = "" ;
var Kingdom_Attitude_Them    = "" ;
var Kingdom_Total_Networth   = 0 ;
var Kingdom_Total_Land       = 0 ;
var Kingdom_Avg_Networth     = 0 ;
var Kingdom_Avg_Land         = 0 ;
var Kingdom_Networth_Rank    = 0 ;
var Kingdom_Land_Rank        = 0 ;
var Kingdom_Total_Kingdoms   = 0 ;
var Kingdom_Avg_NW_Acre      = 0 ;
var Kingdom_Dragon_Cost      = 0 ;
var Kingdom_Min_Dragon_Range = 0 ;
var Kingdom_Max_Dragon_Range = 0 ;
var Kingdom_Stance           = 0 ;
var Kingdom_Wars             = 0 ;
var Kingdom_Wins             = 0 ;

var Kingdom_Province_Online     = new Array(25); for (i=0; i<25 ; i++) Kingdom_Province_Online[i]     = 0;
var Kingdom_Province_Inactive   = new Array(25); for (i=0; i<25 ; i++) Kingdom_Province_Inactive[i]   = 0;
var Kingdom_Province_Protection = new Array(25); for (i=0; i<25 ; i++) Kingdom_Province_Protection[i] = 0;

var Kingdom_Race_Amount = new Array(10);
var temp1 = 0;
var temp2 = 0;

var Order_By_Temp = new Array(25);




var Kingdom_Percentage_Land = new Array(25);
var Kingdom_Percentage_NetW = new Array(25);

Kingdom_Currently_at_WAR = "" ;


mytext = mytext.slice(mytext.indexOf("/Acre"));

Kingdom_Name     = mytext.slice(mytext.indexOf("Current kingdom is ")+19, mytext.indexOf(")")+1);

Kingdom_KD = Kingdom_Name.slice(Kingdom_Name.indexOf("(")+1, Kingdom_Name.indexOf(":"));
Kingdom_IS = Kingdom_Name.slice(Kingdom_Name.indexOf(":")+1, Kingdom_Name.indexOf(")"));

Kingdom_Total_Provinces2 = CB_Find( mytext, "Total Provinces" , " "   );
Kingdom_Total_Networth   = CB_Find( mytext, "Total Networth"  , "gc"  );
Kingdom_Total_Land       = CB_Find( mytext, "Total Land"      , " acres"  );
Kingdom_Avg_Networth     = CB_Find( mytext, "Total Networth " + Kingdom_Total_Networth + "gc (avg:" , "gc" );
Kingdom_Avg_Land         = CB_Find( mytext, "Total Land "     + Kingdom_Total_Land     + " acres (avg:" , " acres" );

Kingdom_Total_Provinces = 25;

if ( mytext.indexOf("Net Worth Rank") != -1 ) {

Kingdom_Networth_Rank    = CB_Find( mytext, "Net Worth Rank"   , " of" );
Kingdom_Land_Rank        = CB_Find( mytext, "Land Rank"       , " of" );
Kingdom_Total_Kingdoms   = CB_Find( mytext, "Net Worth Rank " + Kingdom_Networth_Rank + " of" , eol );

prefService.setIntPref('extensions.Ultima.KingdomYourKDNW' , Kingdom_Total_Networth );

} else {

prefService.setIntPref('extensions.Ultima.KingdomEnemyKDNW' , Kingdom_Total_Networth );

}

if ( mytext.indexOf( "Their Attitude Towards Us:" ) != -1 ) Kingdom_Attitude_Us      = mytext.slice(mytext.indexOf( "Their Attitude Towards Us:" )+27 , mytext.indexOf( "Our Attitude Towards Them:" )-1 );
if ( mytext.indexOf( "Our Attitude Towards Them:" ) != -1 ) Kingdom_Attitude_Them    = mytext.slice(mytext.indexOf( "Our Attitude Towards Them:" )+27 , mytext.indexOf( "Kingdom Stance:"            )-1 );

temp = mytext.slice(mytext.indexOf( "Stance" )+6 , mytext.indexOf( eol ) );

for (i=0; i<4 ; i++) if ( temp == Kingdom_Stance_Search_Key[i] ) Kingdom_Stance = i;

if ( mytext.indexOf("kingdom is currently at war with") != -1 ) {
temp = mytext.slice( mytext.indexOf( "kingdom is currently at war with" )+32 )
Kingdom_Currently_at_WAR = temp.slice( 0 , temp.indexOf( ")" )+1 );
}

mytext = mytext.slice( mytext.indexOf( "Wars Won / Concluded Wars" )+26 );

Kingdom_Wins = Number( mytext.slice( 0 , mytext.indexOf( " " ) ) );

mytext = mytext.slice( mytext.indexOf( "/ " )+2 );

Kingdom_Wars = Number( mytext.slice( 0 , mytext.indexOf( eol ) ) );

mytext = mytext.slice( mytext.indexOf( "Nobility" ) );
mytext = mytext.slice( mytext.indexOf( eol )+1 );
mytext = mytext.replace(/- Awaiting Activation -/g, ":::: (");
mytext = mytext.replace(/- Unclaimed -/g, ":::: (");


temp = mytext;


for (i=0; i<Kingdom_Total_Provinces; i++){

temp = mytext.slice( 0 , mytext.indexOf(eol) );

temp = temp.replace(String.fromCharCode(10),"");
temp = temp.replace(String.fromCharCode(13),"");

Kingdom_Province_Rank[i] =         temp.slice( temp.lastIndexOf("gc "    )+3 );   temp = temp.slice( 0 , temp.lastIndexOf("gc ")    );
Kingdom_Province_NWPA[i] = Number( temp.slice( temp.lastIndexOf("gc "    )+3 ) ); temp = temp.slice( 0 , temp.lastIndexOf("gc ")    );
Kingdom_Province_NetW[i] = Number( temp.slice( temp.lastIndexOf(" acres ")+7 ) ); temp = temp.slice( 0 , temp.lastIndexOf(" acres") );
Kingdom_Province_Land[i] = Number( temp.slice( temp.lastIndexOf(" "      )+1 ) ); temp = temp.slice( 0 , temp.lastIndexOf(" ")      );
Kingdom_Province_Race[i] =         temp.slice( temp.lastIndexOf(" "      )+1 );   temp = temp.slice( 0 , temp.lastIndexOf(" ")      );

if ( Kingdom_Province_Race[i].indexOf( "-" ) != -1 ) {
Kingdom_Province_Land[i] = 0;
Kingdom_Province_NetW[i] = 0;
Kingdom_Province_NWPA[i] = 0;


}



var Your_NW     = Number(prefService.getIntPref('extensions.Ultima.YourThroneNetworth'));
var Your_KD_NW  = Number(prefService.getIntPref('extensions.Ultima.KingdomYourKDNW'));

var Enemy_NW    = Kingdom_Province_NetW[i];
var Enemy_KD_NW = Number(prefService.getIntPref('extensions.Ultima.KingdomEnemyKDNW'));

if ( Your_NW > 0 ) { Relative_NW = Enemy_NW / Your_NW ;
if ( Relative_NW < 0.9 ) Gain_Formula_PNWF = 3 * Relative_NW - 1.7 ;
if (( 0.9 <= Relative_NW )&&( Relative_NW <= 1.1 )) Gain_Formula_PNWF = 1 ;
if ( Relative_NW > 1.1 ) Gain_Formula_PNWF = -2 * Relative_NW + 3.2 ;
} else { Gain_Formula_PNWF = 1 ; }

if ( Your_KD_NW > 0 ) { Relative_KD_NW = Enemy_KD_NW / Your_KD_NW ;
if ( Relative_KD_NW < 0.4 ) Gain_Formula_KNWF = 2/3;
if (( 0.4 <= Relative_KD_NW )&&( Relative_KD_NW <= 0.9 )) Gain_Formula_KNWF = Relative_KD_NW * 2/3 + 0.4 ;
if ( Relative_KD_NW > 0.9 ) Gain_Formula_KNWF = 1 ;
} else { Gain_Formula_KNWF = 1 ; }

if ( Gain_Formula_PNWF < 0 ) Gain_Formula_PNWF = 0 ;

Gain_Formula_Land = Kingdom_Province_Land[i] * 0.12 ;

//Main_Gain_Formula = Gain_Formula_Land * Gain_Formula_PNWF * Gain_Formula_KNWF * Gain_Formula_Race * Gain_Formula_Stance * Gain_Formula_Relations * Gain_Formula_Sciences * Gain_Formula_GS * Gain_Formula_Dragon * Gain_Formula_GBP * Gain_Formula_Time ;

Main_Gain_Formula = Gain_Formula_Land * Gain_Formula_PNWF * Gain_Formula_KNWF;

Kingdom_Province_Gain[i] = Math.round( Main_Gain_Formula );






Kingdom_Province_Info[i] = 0;
Kingdom_Province_Inf2[i] = 0;

if ( Kingdom_Province_Race[i] == "DarkElf" ) Kingdom_Province_Race[i] = "Dark Elf" ;

if ( temp.indexOf( "*" ) != -1 ) {
Kingdom_Province_Inf2[i] = 1;
temp = temp.slice(0,temp.length-1);
Kingdom_Province_Online[i] = 1;
}

if ( temp.indexOf("+") != -1 ) {
Kingdom_Province_Inf2[i] = 2;
temp = temp.slice( 0 , temp.lastIndexOf("+") );
Kingdom_Province_Inactive[i] = 1;
}






if ( temp.indexOf("^") != -1 ) {
Kingdom_Province_Info[i] = 3;
temp = temp.slice( 0 , temp.lastIndexOf("^") );
Kingdom_Province_Protection[i] = 1;
}

if ( temp.indexOf("(S)") != -1 ) {
Kingdom_Province_Info[i] = 4;
temp = temp.slice( 0 , temp.lastIndexOf(" (") );
}

if ( temp.indexOf("(M)") != -1 ) {
Kingdom_Province_Info[i] = 2;
temp = temp.slice( 0 , temp.lastIndexOf(" (") );
}






temp = temp.slice( temp.indexOf(" ") );



Kingdom_Province_Name[i] = temp;

mytext = mytext.slice(mytext.indexOf(eol)+1);

}


for (i=0; i<Kingdom_Total_Provinces; i++) if ( Kingdom_Province_Land[i] != 0 ) Kingdom_Province_Land[i] = Number( Kingdom_Province_Land[i] ) ;
for (i=0; i<Kingdom_Total_Provinces; i++) if ( Kingdom_Province_Land[i] == 0 ) Kingdom_Province_Rank[i] = "Peasant";
for (i=0; i<Kingdom_Total_Provinces; i++) if ( Kingdom_Province_Name[i] == "::::" ) Kingdom_Province_Rank[i] = "Peasant";


for (i=0; i<Kingdom_Total_Provinces; i++) Kingdom_Province_NetW[i] = Number( Kingdom_Province_NetW[i] ) ;
for (i=0; i<Kingdom_Total_Provinces; i++) if ( Kingdom_Province_Land[i] != 0 ) Kingdom_Province_NWPA[i] = Math.round( Kingdom_Province_NetW[i] / Kingdom_Province_Land[i] * 100 ) / 100 ;




for (i=0; i<Kingdom_Total_Provinces; i++) if ( Kingdom_Province_Race[i].charAt(Kingdom_Province_Race[i].length-1) == " " ) Kingdom_Province_Race[i] = Kingdom_Province_Race[i].slice(0,Kingdom_Province_Race[i].length-1);

for (t=0; t<10 ; t++) Kingdom_Race_Amount[t] = 0;

for (i=0; i<Kingdom_Total_Provinces; i++) for (t=0; t<10 ; t++) if ( Kingdom_Province_Race[i] == Race_Name[t] ) {
Kingdom_Province_Race[i] = Race_Name[t];
Kingdom_Race_Amount[t]++;

}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Name[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Race[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Land[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_NetW[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_NWPA[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Rank[i] = i ;
for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Gain[i] = i ;

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_Name[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_Name[i]; Order_By_Name[i] = Order_By_Name[i+1]; Order_By_Name[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_Race[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_Race[i]; Order_By_Race[i] = Order_By_Race[i+1]; Order_By_Race[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_Land[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_Land[i]; Order_By_Land[i] = Order_By_Land[i+1]; Order_By_Land[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_NetW[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_NetW[i]; Order_By_NetW[i] = Order_By_NetW[i+1]; Order_By_NetW[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_NWPA[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_NWPA[i]; Order_By_NWPA[i] = Order_By_NWPA[i+1]; Order_By_NWPA[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Order_By_Temp[i] = Kingdom_Province_Gain[i] ;
for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] < Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_Gain[i]; Order_By_Gain[i] = Order_By_Gain[i+1]; Order_By_Gain[i+1] = temp2;
}

for (m=0; m<Kingdom_Total_Provinces; m++) for (t=0; t<2 ; t++) for (i=0; i<10 ; i++) if ( Kingdom_Province_Rank[m] == Rank_Name[t][i] ) {

Order_By_Temp[m] = 9-i;

if (( Kingdom_Province_Info[m] != 2 )&&( Kingdom_Province_Info[m] != 3 )&&( Kingdom_Province_Info[m] != 4 )&&( i != 0 )) Kingdom_Province_Info[m] = t;

}


for (t=0; t<Kingdom_Total_Provinces; t++) for (i=0; i<Kingdom_Total_Provinces-1; i++) if ( Order_By_Temp[i] > Order_By_Temp[i+1] ) {
temp1 = Order_By_Temp[i]; Order_By_Temp[i] = Order_By_Temp[i+1]; Order_By_Temp[i+1] = temp1;
temp2 = Order_By_Rank[i]; Order_By_Rank[i] = Order_By_Rank[i+1]; Order_By_Rank[i+1] = temp2;
}

for (i=0; i<Kingdom_Total_Provinces; i++) Kingdom_Percentage_Land[i] = Math.round( Kingdom_Province_Land[i] / Kingdom_Total_Land     * 1000 ) /10;
for (i=0; i<Kingdom_Total_Provinces; i++) Kingdom_Percentage_NetW[i] = Math.round( Kingdom_Province_NetW[i] / Kingdom_Total_Networth * 1000 ) /10;

Kingdom_Avg_NW_Acre      = Math.round( Kingdom_Total_Networth / Kingdom_Total_Land * 100 ) / 100 ;
Kingdom_Dragon_Cost      = Math.round( Kingdom_Total_Networth * 1.25 ) ;
Kingdom_Min_Dragon_Range = Math.round( Kingdom_Total_Networth / 1.25 ) ;
Kingdom_Max_Dragon_Range = Math.round( Kingdom_Total_Networth * 1.25 ) ;

for (i=0; i<Kingdom_Total_Provinces; i++) Kingdom_Province_Land[i] = Add_Commas( Kingdom_Province_Land[i] );
for (i=0; i<Kingdom_Total_Provinces; i++) Kingdom_Province_NetW[i] = Add_Commas( Kingdom_Province_NetW[i] );

Kingdom_Total_Provinces  = Add_Commas( Kingdom_Total_Provinces  );
Kingdom_Total_Networth   = Add_Commas( Kingdom_Total_Networth   );
Kingdom_Total_Land       = Add_Commas( Kingdom_Total_Land       );
Kingdom_Avg_Networth     = Add_Commas( Kingdom_Avg_Networth     );
Kingdom_Avg_Land         = Add_Commas( Kingdom_Avg_Land         );
Kingdom_Networth_Rank    = Add_Commas( Kingdom_Networth_Rank    );
Kingdom_Land_Rank        = Add_Commas( Kingdom_Land_Rank        );
Kingdom_Total_Kingdoms   = Add_Commas( Kingdom_Total_Kingdoms   );
Kingdom_Avg_NW_Acre      = Add_Commas( Kingdom_Avg_NW_Acre      );
Kingdom_Dragon_Cost      = Add_Commas( Kingdom_Dragon_Cost      );
Kingdom_Min_Dragon_Range = Add_Commas( Kingdom_Min_Dragon_Range );
Kingdom_Max_Dragon_Range = Add_Commas( Kingdom_Max_Dragon_Range );

textonly=""+c0+ Kingdom_Name    +" Kingdom Analysis"+cc+Copyrights+
"<br />"+
"<br />"+c0+ "** Statistics **"+cc+
"<br />"+c1+ "Provinces in Kingdom: "      +cc+c2+ Kingdom_Total_Provinces2 +cc;

if ( Kingdom_Land_Rank        != 0 ) textonly += "<br />"+c1+ "Total Land: "            +cc+c2+ Kingdom_Total_Land       +cc+c1+ " Acres (avg: "+ Kingdom_Avg_Land +" Acres, Rank #"+ Kingdom_Land_Rank +")" +cc;
if ( Kingdom_Networth_Rank    != 0 ) textonly += "<br />"+c1+ "Networth: "              +cc+c2+ Kingdom_Total_Networth   +cc+c1+ "gc (avg: "+ Kingdom_Avg_Networth +"gc, Rank #"+ Kingdom_Networth_Rank +")" +cc;

if ( Kingdom_Land_Rank        == 0 ) textonly += "<br />"+c1+ "Total Land: "            +cc+c2+ Kingdom_Total_Land       +cc+c1+ " Acres (Average: "+ Kingdom_Avg_Land +" Acres)" +cc;
if ( Kingdom_Networth_Rank    == 0 ) textonly += "<br />"+c1+ "Total Networth: "        +cc+c2+ Kingdom_Total_Networth   +cc+c1+ "gc (Average: "+ Kingdom_Avg_Networth +"gc)" +cc;

textonly += "<br />"+c1+ "Average Networth per Acre: " +cc+c2+ Kingdom_Avg_NW_Acre      +cc+c1+ "gc" +cc+

"<br /><br />"+c0+ "** Dragon **"+cc+

"<br />"+c1+ "Dragon Cost: "               +cc+c2+ Kingdom_Dragon_Cost      +cc+c1+ "gc" +cc+
"<br />"+c1+ "Dragon Send Range: "         +cc+c2+ Kingdom_Min_Dragon_Range +cc+c1+ "gc - " +cc+c2+ Kingdom_Max_Dragon_Range +cc+c1+ "gc" +cc+

"<br /><br />"+c0+ "** Relations **"+cc;

if ( Kingdom_Currently_at_WAR != "" ) textonly += "<br />"+c1+ "Currently at WAR with: "     +cc+c2+ Kingdom_Currently_at_WAR +cc;

if ( Kingdom_Attitude_Us != "" ) { textonly += "<br />"+c1+ "Attitude towards/from Us: "  +cc+c2+ Kingdom_Attitude_Us +cc;
if ( Kingdom_Attitude_Us != Kingdom_Attitude_Them ) textonly += c1+" / "+cc+c2+ Kingdom_Attitude_Them +cc;
}

textonly += "<br />"+c1+ "Stance: " +cc+c2+ Kingdom_Stance_Search_Key[Kingdom_Stance] +cc+c1+" "+ Kingdom_Stance_Name[Kingdom_Stance] +cc+
"<br />"+c1+ "War History: " +cc+c2+ Kingdom_Wars +cc+c1+ " Wars" +cc+c1+", "+cc+c2+ Kingdom_Wins +cc+c1+ " Wins" +cc;
if ( Kingdom_Wins != 0 ) textonly += c1+" ("+ Math.round( Kingdom_Wins / Kingdom_Wars * 100 ) +"%)" +cc;

temp1 = 0; textonly += "<br /><br />"+c0+ "** Races **"             +cc; for (i=0; i<10; i++) if ( Kingdom_Race_Amount[i] > 0 ) { temp1++; textonly +="<br />"+c1+ Math.round(temp1) + ". " + Race_Name[i] +": "+cc+c2+ Kingdom_Race_Amount[i] +cc+c1+" ("+ Math.round( Kingdom_Race_Amount[i] / Kingdom_Total_Provinces * 100 ) +"%)"+cc; }
temp1 = 0; textonly += "<br /><br />"+c0+ "** Land **"              +cc; for (i=0; i<Kingdom_Total_Provinces; i++) if (( Kingdom_Province_Name[Order_By_Land[i]] != "::::" )&&( Kingdom_Province_Land[Order_By_Land[i]] != 0 )) { temp1++; textonly +="<br />"+c1+ Math.round(temp1) + ". " + Kingdom_Province_Name[Order_By_Land[i]] +" ["+ Kingdom_Province_Race[Order_By_Land[i]] + "] - " +cc+c2+ Kingdom_Province_Land[Order_By_Land[i]] +cc+c1+ " Acres (" + Kingdom_Percentage_Land[Order_By_Land[i]] + "%)" +cc; }
temp1 = 0; textonly += "<br /><br />"+c0+ "** Networth **"          +cc; for (i=0; i<Kingdom_Total_Provinces; i++) if (( Kingdom_Province_Name[Order_By_NetW[i]] != "::::" )&&( Kingdom_Province_Land[Order_By_NetW[i]] != 0 )) { temp1++; textonly +="<br />"+c1+ Math.round(temp1) + ". " + Kingdom_Province_Name[Order_By_NetW[i]] +" ["+ Kingdom_Province_Race[Order_By_NetW[i]] + "] - " +cc+c2+ Kingdom_Province_NetW[Order_By_NetW[i]] +cc+c1+ "gc ("     + Kingdom_Percentage_NetW[Order_By_NetW[i]] + "%)" +cc; }
temp1 = 0; textonly += "<br /><br />"+c0+ "** Networth per Acre **" +cc; for (i=0; i<Kingdom_Total_Provinces; i++) if (( Kingdom_Province_Name[Order_By_NWPA[i]] != "::::" )&&( Kingdom_Province_Land[Order_By_NWPA[i]] != 0 )) { temp1++; textonly +="<br />"+c1+ Math.round(temp1) + ". " + Kingdom_Province_Name[Order_By_NWPA[i]] +" ["+ Kingdom_Province_Race[Order_By_NWPA[i]] + "] - " +cc+c2+ Kingdom_Province_NWPA[Order_By_NWPA[i]] +cc+c1+ "gc" +cc; }
temp1 = 0; textonly += "<br /><br />"+c0+ "** Ranks **"             +cc; for (i=0; i<Kingdom_Total_Provinces; i++) if (( Kingdom_Province_Name[Order_By_Rank[i]] != "::::" )&&( Kingdom_Province_Land[Order_By_Rank[i]] != 0 )) { temp1++; textonly +="<br />"+c1+ Math.round(temp1) + ". " + Kingdom_Province_Name[Order_By_Rank[i]] +" ["+ Kingdom_Province_Race[Order_By_Rank[i]] + "] - " +cc+c2+ Kingdom_Province_Rank[Order_By_Rank[i]] +cc; }

textonly += "<br /><br />"+c0+ "** Miscellaneous **"+cc;

for (i=0; i<25; i++) if ( Kingdom_Province_Online[i] == 1 ) if (( Kingdom_Province_Name[i] != "::::" )&&( Kingdom_Province_Land[i] != 0 )) textonly +="<br />"+c2+ "ONLINE: " +cc+c1+ Kingdom_Province_Name[i] + " [" + Kingdom_Province_Race[i] + "]" +cc;

for (i=0; i<25; i++) if ( Kingdom_Province_Inactive[i] == 1 ) if (( Kingdom_Province_Name[i] != "::::" )&&( Kingdom_Province_Land[i] != 0 )) textonly +="<br />"+c2+ "INACTIVE: " +cc+c1+ Kingdom_Province_Name[i] + " [" + Kingdom_Province_Race[i] + "]" +cc;

for (i=0; i<25; i++) if ( Kingdom_Province_Protection[i] == 1 ) if (( Kingdom_Province_Name[i] != "::::" )&&( Kingdom_Province_Land[i] != 0 )) textonly +="<br />"+c2+ "PROTECTION: " +cc+c1+ Kingdom_Province_Name[i] + " [" + Kingdom_Province_Race[i] + "]" +cc;

//for (i=0; i<25; i++) if ( Kingdom_Province_Land[i] == 0 ) textonly +="<br />"+c2+ "DEAD: " +cc+c1+ Kingdom_Province_Name[i] + " [" + Kingdom_Province_Race[i] + "]" +cc;


textonly += "<br />";

formated_text = textonly;

textonly = textonly.replace(/<font color='#aa8822'>/g, "");
textonly = textonly.replace(/<font>/g, "");
textonly = textonly.replace(/<font color='#888888'>/g, "");
textonly = textonly.replace(/<font color='#ff0000'>/g, "");
textonly = textonly.replace(/<\/font>/g, "");
textonly = textonly.replace(/<br \/>/g, String.fromCharCode(13,10));
textonly = textonly.replace(/  /g, " ");

formated_copy = textonly;

}

function sortkdtable(order) {

var output='';
var temp='';
var i2=0;

if ( order != kdsortorder ) { ascending = true; kdsortorder = order; } else { if (ascending) ascending = false; else ascending = true; }

for (i=0; i<Kingdom_Total_Provinces; i++) {

if (order == 0) temp = i;

if (order == 1) if (ascending) temp = Order_By_Name[i]; else temp = Order_By_Name[Kingdom_Total_Provinces-1-i];
if (order == 2) if (ascending) temp = Order_By_Race[i]; else temp = Order_By_Race[Kingdom_Total_Provinces-1-i];
if (order == 3) if (ascending) temp = Order_By_Land[i]; else temp = Order_By_Land[Kingdom_Total_Provinces-1-i];
if (order == 4) if (ascending) temp = Order_By_NetW[i]; else temp = Order_By_NetW[Kingdom_Total_Provinces-1-i];
if (order == 5) if (ascending) temp = Order_By_NWPA[i]; else temp = Order_By_NWPA[Kingdom_Total_Provinces-1-i];
if (order == 6) if (ascending) temp = Order_By_Rank[i]; else temp = Order_By_Rank[Kingdom_Total_Provinces-1-i];
if (order == 7) if (ascending) temp = Order_By_Gain[i]; else temp = Order_By_Gain[Kingdom_Total_Provinces-1-i];

if (( Kingdom_Province_Name[temp] != "::::" )&&( Kingdom_Province_Land[temp] != 0 )) {

i2++;

output += '<tr valign="top"';

//if ( i2 % 2 == 0 ) {
//if ( Kingdom_Currently_at_WAR == "" ) output += ' style="background:#101010;"'; else output += ' style="background:#100000;"'; } else {
//if ( Kingdom_Currently_at_WAR == "" ) output += ' style="background:#000000;"'; else output += ' style="background:#000000;"';
//}

if ( i2 % 2 == 0 ) if ( Kingdom_Currently_at_WAR != "" ) output += ' style="background-color:rgba(128, 0, 0, 0.2)"';

//if ( prefService.getCharPref('extensions.Ultima.YourThroneProvinceName') == Kingdom_Province_Name[temp] ) output += ' class="you"';
if ( Kingdom_Province_Info[temp] == 2 ) output += ' class="monarch"';
if ( Kingdom_Province_Info[temp] == 3 ) output += ' class="protected"';
if ( Kingdom_Province_Info[temp] == 4 ) output += ' class="chamberlain"';

output += '>';

if ( 1==2) {

output += '<td>';

if ( Kingdom_Province_Info[temp] == 0 ) output += '<img src="chrome://Ultima/content/Male.gif"       alt="" />';
if ( Kingdom_Province_Info[temp] == 1 ) output += '<img src="chrome://Ultima/content/Female.gif"     alt="" />';
if ( Kingdom_Province_Info[temp] == 2 ) output += '<img src="chrome://Ultima/content/Crown.gif"      alt="" />';
if ( Kingdom_Province_Info[temp] == 3 ) output += '<img src="chrome://Ultima/content/Protection.gif" alt="" />';

output += '</td>';

}

if ( kdsortorder0 ) {

     if ( Kingdom_Province_Inf2[temp] == 0 ) output += '<td width="30"><img src="chrome://Ultima/content/Offline.png"  alt="" /></td>';
else if ( Kingdom_Province_Inf2[temp] == 1 ) output += '<td width="30"><img src="chrome://Ultima/content/Online.png"   alt="" /></td>';
else if ( Kingdom_Province_Inf2[temp] == 2 ) output += '<td width="30"><img src="chrome://Ultima/content/Inactive.gif" alt="" /></td>';

}

if ( kdsortorder1 ) output += '<td class="province-name"><a href="/wol/game/province_profile/'+Kingdom_KD+'/'+Kingdom_IS+'/'+Math.round(temp+1)+'">'+Kingdom_Province_Name[temp]+'</a></td>';
if ( kdsortorder2 ) output += '<td valign="middle">'+Kingdom_Province_Race[temp]+'</td>';
if ( kdsortorder3 ) output += '<td>'+Kingdom_Province_Land[temp]+'</td>';
if ( kdsortorder4 ) output += '<td>'+Kingdom_Province_NetW[temp]+'</td>';
if ( kdsortorder5 ) output += '<td>'+Kingdom_Province_NWPA[temp]+'</td>';
if ( kdsortorder6 ) output += '<td>'+Kingdom_Province_Rank[temp]+'</td>';
if ( kdsortorder7 ) output += '<td>'+Kingdom_Province_Gain[temp]+'</td>';

output += '</tr>';

}

}

if (kdtable) kdtable.innerHTML = output;

}
